var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var hasMore = true;
var goinMore = true;
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tc: 0,
    mingxiData: [],
    token: '',
    uid: 0,

    colorSet: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tc = app.get_tc();
    that.setData({
      tc: tc
    })
    var titlename = tc == 1 ? '收支明細' : '收支明细';
    wx.setNavigationBarTitle({
      title: titlename,
    });
    
    app.LoginUserInfo(function (res) {
      that.setData({
        uid: res.uid,
        token: res.token,
      });
      that.loadMingxi(1);
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });
  },
  //设置titlebar 样式
  resetNavColor: function () {
    // var bg_color = '#f0f0f0';
    // var fg_color = '#000000';
    // if (this.data.colorSet) {
    //   bg_color = this.data.colorSet.titlebg;
    //   fg_color = this.data.colorSet.titlefont;
    // }
    // wx.setNavigationBarColor({
    //   frontColor: fg_color,
    //   backgroundColor: bg_color
    // })
  },

  loadMore: function(){
    if (hasMore && goinMore) {
      goinMore = false;
      page++;
      this.loadMingxi(page);
    } else {
      console.log("文字没有更多了");
    }
  },

  loadMingxi: function (cpage) {
    var that = this;
    util.api({
      method: "POST",
      data: {
        ac: "changelist",
        uid: that.data.uid,
        page: cpage,
        num: 20,
        "privatekey": utilMd5.hexMD5(that.data.uid + 'changelist' + that.data.token)
      },
      success: function (responseData) {
        var result = responseData.data;
        console.log("mingxi result " , result);
        if (result && result.length > 0) {
          for(var i = 0; i < result.length; i++){
            var date = new Date(result[i].ctime * 1000);
            result[i].formatTime = util.formatTime(date, '-', ':');
            var coinnum = result[i].coinnum;
            result[i].formatCoinnum = (coinnum/100.0).toFixed(2);
          }
          that.setData({
              mingxiData: that.data.mingxiData.concat(result),
          });
        } else {
          hasMore = false;
        }
        goinMore = true;
      }
    });
  },
})